
//宫格 start
function grid(obj, options) {
    var self = this;

    this.target = '';          //中奖位置 

    //设置默认选项
    options = options || {};
    options.waiting = options.waiting || 8000;     //匀速转动时长 
    options.index = options.index || 0;            //初始位置

    var initSpeed = 500; //初始转动速度 
    var speed = 50;      //当前转动速度 
    var upStep = 50;     //加速滚动步长 
    var upMax = 50;      //速度上限 
    var downStep = 30;   //减速滚动步长 
    var downMax = 500;   //减速上限 
    var isRunning = false;//当前是否正在抽奖 

    var aChild = document.querySelector(obj).children;
    if (aChild.length == 0) {
        throw "没发现Dom, can't grid!!!";
    }

    var len = aChild.length; // 奖品格子
    var Timer = null;

    //逻辑操作
    this._index = function () {// 转盘当前格子下标
        return options.index % len;
    };

    aChild[self._index()].classList.add('active');

    this.start = function () {// 开启抽奖
        if (lucky.isGridBegin) {
            return;
        }
        lucky.isGridBegin = true;
        console.log('开始');
        _roll();

        //检测是否有目标值
        var time = 0;
        Timer = setInterval(function () {
            time++;
            console.log(time);
            if (time * 1000 >= options.waiting) {
                console.log("超时");
                _stop();
                _roll();
                time = 0;
            }
            if (self.target || self.target == 0) {
                console.log("有值了");
                clearInterval(Timer);
            }
        }, 1000);
    };

    this.stop = function () {// 转盘停止，还原设置
        clearTimeout(self.downTimer);
        clearTimeout(self.rollerTimer);
        speed = initSpeed;
        isRunning = false;
        lucky.isGridBegin = false;
        self.target = '';
        options.onEnd && options.onEnd();
        clearInterval(Timer);
    };

    function _roll() {// 转盘滚动
        aChild[self._index()].classList.remove('active');
        ++options.index;
        aChild[self._index()].classList.add('active');
        self.rollerTimer = setTimeout(function () { _roll(); }, speed);
        if (!isRunning) {
            _up();
            isRunning = true;
        }
    };

    function _up() {// 转盘加速
        if (speed <= upMax) {
            _constant();
        } else {
            speed -= upStep;
            self.upTimer = setTimeout(function () { _up(); }, speed);
        }
    };

    function _constant() {// 转盘匀速
        clearTimeout(self.upTimer);
        setTimeout(function () { _down(); }, options.waiting);
    };

    function _down() {// 转盘减速
        if (speed > downMax && self.target == self._index()) {
            _stop();
        } else {
            speed += downStep;
            self.downTimer = setTimeout(function () { _down(); }, speed);
        }
    };

    function _stop() {// 转盘停止，还原设置
        clearTimeout(self.downTimer);
        clearTimeout(self.rollerTimer);
        speed = initSpeed;
        isRunning = false;
        if (self.target || self.target == 0) {
            lucky.isGridBegin = false;
            self.target = '';
            options.onEnd && options.onEnd();
            clearInterval(Timer);
        }
    };

}

//宫格 end